﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLogic.StateManagers;

namespace BusinessLogic.Model.Repository.CommentsRepositories
{
    class CommentRepository : Repository, ICommentRepository
    {
        internal CommentRepository(BlogNETDataContext dataContext) : base(dataContext)
        {
        }

        public void Create(Comment comment)
        {
            //Transaction is needed to avoid breaking data integrity between the two SubmitChanges() calls
            //This is very unlikely to occur so I'll just let it pop as a general error.
            try
            {
                DataContext.Connection.Open();
                DataContext.Transaction = DataContext.Connection.BeginTransaction();

                DataContext.Comments.InsertOnSubmit(comment);
                comment.NewsItem.CommentsCount++;
                DataContext.SubmitChanges();

                //Need to update the LastCommentID and submit again
                comment.NewsItem.LastCommentID = comment.CommentID;
                DataContext.SubmitChanges();

                DataContext.Transaction.Commit();
            }
            catch (Exception)
            {
                if (DataContext.Transaction != null)
                {
                    DataContext.Transaction.Rollback();
                }

                throw;
            }
            finally
            {
                if (DataContext.Connection.State == System.Data.ConnectionState.Open)
                {
                    DataContext.Connection.Close();
                }
            }

            CacheManager.RemoveLatestComments();
        }

        public Comment Get(int commentID)
        {
            return (from c in DataContext.Comments
                    where c.CommentID == commentID
                    select c).SingleOrDefault();
        }

        public void Delete(int commentID)
        {
            Delete(Get(commentID));
        }

        public void Delete(Comment comment)
        {
            //if we are not deleting the last comment updating the LastCommentID is not needed
            NewsItem newsItem = comment.NewsItem;
            if (comment.CommentID != newsItem.LastCommentID)
            {
                DataContext.Comments.DeleteOnSubmit(comment);
                newsItem.CommentsCount--;
                DataContext.SubmitChanges();
            }
            else
            {
                //Transaction is needed to avoid breaking data integrity between the two SubmitChanges() calls
                //This is very unlikely to occur so I'll just let it pop as a general error.
                try
                {
                    DataContext.Connection.Open();
                    DataContext.Transaction = DataContext.Connection.BeginTransaction();

                    DataContext.Comments.DeleteOnSubmit(comment);
                    newsItem.CommentsCount--;
                    DataContext.SubmitChanges();

                    //Need to update the LastCommentID and submit again
                    newsItem.LastCommentID = (from c in newsItem.Comments
                                              orderby c.CommentID descending
                                              select c.CommentID).Take(1).SingleOrDefault();

                    if (newsItem.LastCommentID == 0)
                    {
                        newsItem.LastCommentID = null;
                    }

                    DataContext.SubmitChanges();

                    DataContext.Transaction.Commit();
                }
                catch (Exception)
                {
                    if (DataContext.Transaction != null)
                    {
                        DataContext.Transaction.Rollback();
                    }

                    throw;
                }
                finally
                {
                    if (DataContext.Connection.State == System.Data.ConnectionState.Open)
                    {
                        DataContext.Connection.Close();
                    }
                }
            }
        }

        public IQueryable<Comment> GetByNewsItem(int newsItemID)
        {
            return from comment in DataContext.Comments
                       orderby comment.CommentID ascending
                       where comment.NewsItemID == newsItemID
                       select comment;
        }
    }
}
